package com.jiyun.controller;

import com.jiyun.pojo.entity.Order;
import com.jiyun.service.FaDanService;
import com.jiyun.util.OSSUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS})
//@Controller
@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private FaDanService faDanService;

    @RequestMapping("image")
    public String image(MultipartFile file, Order order) throws Exception {
        if (order.getOid()!=null){
            if (order.getOrderimage()==null){
                String filename = file.getOriginalFilename();
                String sx = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                String url = OSSUploadUtil.uploadFile(file.getInputStream(), sx);
                order.setOrderimage(url);
            }
            faDanService.updateBYoid(order);
        }else {
            String filename = file.getOriginalFilename();
            String sx = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            String url = OSSUploadUtil.uploadFile(file.getInputStream(), sx);
            order.setOid(null);
            order.setOrderimage(url);
            faDanService.Fandan(order);
        }
        return "提交成功请返回查看";

    }

}
